import 'package:contract_manager/common/langs/translation_service.dart';
import 'package:contract_manager/global.dart';
import 'package:contract_manager/models/contract_model.dart';
import 'package:contract_manager/pages/login/login_binding.dart';
import 'package:contract_manager/pages/login/login_view.dart';
import 'package:contract_manager/router/app_pages.dart';
import 'package:contract_manager/services/contract_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:realm/realm.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Configure Realm with migration
  final config = Configuration.local([Contract.schema], schemaVersion: 1,
      migrationCallback: (migration, oldSchemaVersion) {
    if (oldSchemaVersion < 1) {
      migration.newRealm.deleteAll();
    }
  });

  await Global.init(config: config).then((e) => runApp(MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // Initialize ContractService
    Get.put(ContractService());

    return GetMaterialApp(
      title: 'Flutter With GetX',
      home: LoginPage(),
      initialBinding: LoginBinding(),
      debugShowCheckedModeBanner: false,
      enableLog: true,
      initialRoute: AppPages.INITIAL,
      getPages: AppPages.routes,
      unknownRoute: AppPages.unknownRoute,
      builder: EasyLoading.init(),
      locale: TranslationService.locale,
      fallbackLocale: TranslationService.fallbackLocale,
    );
  }
}
